﻿#include <easyx.h>
#include <time.h>
#include <stdio.h>
#include <math.h>

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define MAX_IN_WINDOW 5//气球数量
#define BALLOON_RADIUS 30//气球的半径

//打气球小游戏
typedef struct {
	int x;
	int y;
	int r;
	int v;
	COLORREF color;
}balloon;


//生成气球的函数

balloon generateBalloon()
{
	balloon b;
	int m, n;
	m = 100;
	n = 700;
	b.x = rand() % (n - m + 1) + m;
	b.y = WINDOW_HEIGHT;
	b.r = BALLOON_RADIUS;
	m = 1; n = 3;
	b.v = rand() % (n - m + 1) + m;
	b.color = RGB(rand() % 256, rand() % 256, rand() % 256);
	return b;
}


int main()
{
	initgraph(WINDOW_WIDTH, WINDOW_HEIGHT);
	setbkcolor(WHITE);
	cleardevice();
	balloon arrBalloons[MAX_IN_WINDOW];
	int current = MAX_IN_WINDOW;
	int mouseX = 0;
	int mouseY = 0;


		for (int i = 0; i < MAX_IN_WINDOW; i++)
		{
			//int m, n;
			//m = 100;
			//n = 700;
			//arrBalloons[i].x = rand() % (n - m + 1) + m;
			//arrBalloons[i].y = WINDOW_HEIGHT;
			//arrBalloons[i].r = BALLOON_RADIUS;
			//m = 1; n = 3;
			//arrBalloons[i].v = rand() % (n - m + 1) + m;
			//arrBalloons[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
			arrBalloons[i] = generateBalloon();
		}
		timeBeginPeriod(1);
		LARGE_INTEGER startCount, endCount, F;
		QueryPerformanceFrequency(&F);
		BeginBatchDraw();
	while (1)
	{

		QueryPerformanceCounter(&startCount);
		//准备画面
		cleardevice();
		for (int i = 0; i < MAX_IN_WINDOW; i++)
		{
			setfillcolor(arrBalloons[i].color);
			solidcircle(arrBalloons[i].x, arrBalloons[i].y, arrBalloons[i].r);

		}
		for (int i = 0; i < MAX_IN_WINDOW; i++)
		{
			arrBalloons[i].y -= arrBalloons[i].v;

		}
		//删除飞出窗面的气球
		int i = 0;
		while (i < current)//依次遍历整个数组的气球
		{
			int y = arrBalloons[i].y;
			if (y < -BALLOON_RADIUS)
			{
				for (int j = i; j < current - 1; j++)
				{
					arrBalloons[j] = arrBalloons[j + 1];

				}
				current--;
			}
			else
			{
				i++;
			}

		}
		if (current < MAX_IN_WINDOW)
		{
			//int m, n;
			//m = 100;
			//n = 700;
			//arrBalloons[current].x = rand() % (n - m + 1) + m;
			//arrBalloons[current].y = WINDOW_HEIGHT;
			//arrBalloons[current].r = BALLOON_RADIUS;
			//m = 1; n = 3;
			//arrBalloons[current].v = rand() % (n - m + 1) + m;
			//arrBalloons[current].color = RGB(rand() % 256, rand() % 256, rand() % 256);
			//current++;
			arrBalloons[current] = generateBalloon();
			current++;
		}
		setlinecolor(RGB(237, 178, 29));
		setlinestyle(PS_SOLID, 3);
		circle(mouseX, mouseY, 20);
		line(mouseX - 20, mouseY, mouseX + 20, mouseY);
		line(mouseX, mouseY - 20, mouseX, mouseY + 20);


		
		QueryPerformanceCounter(&endCount);
		long long elapse = (endCount.QuadPart - startCount.QuadPart) * 1000000 / F.QuadPart;
		while (elapse < 1000000 / 60)
		{
			Sleep(1);

			//getmessage(&msg, EX_MOUSE);

			ExMessage msg;
			bool isOK = peekmessage(&msg, EX_MOUSE);
			if (isOK == true)
			{
				if (msg.message == WM_MOUSEMOVE)
				{
					mouseX = msg.x;
					mouseY = msg.y;
				}
				else if (msg.message == WM_LBUTTONDOWN)
				{
					int i = 0;
					while (i < current)
					{
						int ballX = arrBalloons[i].x;
						int ballY = arrBalloons[i].y;
						int distance = (int)sqrt(pow(ballY - msg.y, 2) + pow(ballX - msg.x, 2));
						if (distance < BALLOON_RADIUS)
						{
							for (int j = i; j < current - 1; j++)
							{
								arrBalloons[j] = arrBalloons[j + 1];

							}
							current--;
						}
						else
						{
							i++;
						}
					}
				}
			}



			QueryPerformanceCounter(&endCount);
			elapse = (endCount.QuadPart - startCount.QuadPart) * 1000000 / F.QuadPart;
		}
		FlushBatchDraw();
	}

		


		


	EndBatchDraw();
	timeEndPeriod(1);


	return 0;
}
